package com.shenglue.ssi.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.BuyInMain;
import com.shenglue.ssi.domain.Exportable;
import com.shenglue.ssi.domain.Supp;
import com.shenglue.ssi.service.BuyInMainService;

public class BuyInMainAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private BuyInMain buyInMain;	// 对应的 Bean
	private int buyInMainId;		// 删除/修改 的 记录之Id
	private BuyInMainService buyInMainService; // 依赖的 Service
	private int maintainMode = 1; // 1-增删改 ； 2-维护结账信息   。实际从url参数传递进来

	/**
	 * 查询所有入库单主表信息方法
	 */
	public String queryAll() throws Exception {
		List<BuyInMain> list = buyInMainService.selectAllBuyInMain();
		if( list != null ) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "buyInMainList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}

		boolean flag = queryAllSupp( 1 );
		if( flag == false ) {
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 按条件查询 入库单主表信息方法
	 */
	public String selectBuyInMainByCond() throws Exception {
		List<BuyInMain> list = buyInMainService.selectBuyInMainByCond ( buyInMain );
		if( list != null ) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "buyInMainList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}

		boolean flag = queryAllSupp( 1 );
		if( flag == false ) {
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 增加入库单主表记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = buyInMainService.saveBuyInMain( buyInMain );
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 删除入库单主表记录方法
	 */
	public String delete() throws Exception {

		boolean flag = buyInMainService.deleteBuyInMain( buyInMainId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改入库单主表记录方法
	 */
	public String update() throws Exception {

		boolean flag = buyInMainService.updateBuyInMain( buyInMain );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}


	/**
	 * 准备 转向 新增 入库单主表记录 的界面
	 */
	public String prepareAddBuyInMain() {
		boolean flag = queryAllSupp( 0 );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 准备 转向 修改 入库单主表记录 的界面
	 */
	public String prepareModBuyInMain() {
		boolean flag = queryAllSupp( 0 );
		if( flag == false ) {
			return ERROR;
		}

		buyInMain = buyInMainService.getOneBuyInMainForUpdate( buyInMainId );
		if( buyInMain == null ) {
			return ERROR;
		}

		return SUCCESS;
	}



	/**
	 * 预先查好 供应商，为了制作下拉列表框
	 * @param workMode  0: 普通查询 ； 1: 在list的最前面，加一个空对象，以允许用户在选择时，不选 任何供应商
	 * @return
	 */
	private boolean queryAllSupp( int workMode ) {
		boolean bRtn;
		List<Supp> qrySupp = buyInMainService.selectAllSupp();
		if( qrySupp != null ){
			List<Supp> reqSupp = new ArrayList<Supp>(); // 最终放入 request 的
			if ( workMode == 1 ) {
				reqSupp.add( new Supp() );
				reqSupp.addAll( qrySupp );
			} else {
				reqSupp = qrySupp;
			}
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "allSupp", reqSupp );
			bRtn = true;
		}else{
			bRtn = false;
		}
		return bRtn;
	}

	/**
	 * 查询 入库单主表，并导出到 Excel
	 */
	public String queryAndExportExcel() throws Exception {
		fileName = "BuyInMain__入库单信息";

		HttpServletRequest request = ServletActionContext.getRequest();

		List list = buyInMainService.selectAllBuyInMain(); // 此处 无法使用 泛型。 BuyInMain、Exportable 都不行
		if( list == null ) {
			request.setAttribute( "message", "没有数据" );
			return ERROR;
		}

		HSSFWorkbook workbook = getWorkbook( list ); // 此处 无法使用 泛型。 BuyInMain、Exportable 都不行

		if( workbook == null ) {
			request.setAttribute("message", "创建Excel失败");
			return ERROR;
		}

		try{

			//第四步：将工作簿写入最上面定义的InputStream流——名称为excelStream，这个名字对应struts.xml中配置的inputName参数
			this.workbook2InputStream( workbook, fileName );
			return SUCCESS;
		}catch(IOException e){
			e.printStackTrace();
			request.setAttribute("message", "创建Excel失败");
			return ERROR;
		}

	}

	public BuyInMainAction() {
	}

	public BuyInMain getBuyInMain() {
		return buyInMain;
	}

	public void setBuyInMain(BuyInMain buyInMain) {
		this.buyInMain = buyInMain;
		// String handler = buyInMain.getHandler();
		// String rightHandler;
		// try {
		// 	rightHandler = new String( handler.getBytes("iso-8859-1"), "utf-8" );
		// } catch (UnsupportedEncodingException e) {
		// 	rightHandler = "";
		// 	e.printStackTrace();
		// }
		// this.buyInMain.setHandler( rightHandler );
	}

	public int getBuyInMainId() {
		return buyInMainId;
	}

	public void setBuyInMainId(int buyInMainId) {
		this.buyInMainId = buyInMainId;
	}

	public BuyInMainService getBuyInMainService() {
		return buyInMainService;
	}

	public void setBuyInMainService(BuyInMainService buyInMainService) {
		this.buyInMainService = buyInMainService;
	}

	public int getMaintainMode() {
		return maintainMode;
	}

	public void setMaintainMode(int maintainMode) {
		this.maintainMode = maintainMode;
	}

}
